<?php

/**
 * 运单申报服务中心
 */
namespace App\Service\DeclareService;
use Service\BaseService;
use App\Service\AppService\SystemCacheConfigService;
use App\Service\Code\Normal;
use App\Model\PssModel\OrderSupplier;
class WayBillDecareService extends BaseService
{
    //申报服务路由
    protected $routerMap = [
        'haikou'                => "App\\Service\\VendorClient\\OrderWayBillDeclareServiceApi\\",
        'ShopCaiNiaoDeclareBc'  => "App\\Service\\VendorClient\\ShopCaiNiao\\"
    ];
    
    /**
     * 服务申报上传
     * @param array $binfo
     * @param array $order
     * @param array $orderSupplier
     * @param array $orderGoods
     * @param array $supplier
     */
    public function upload(array $binfo,array $order,array $orderSupplier,array $orderGoods,array $supplier)
    {
        if( !getIsTomoMode())  {
            return Normal::returnCode(Normal::SUC,"当前业务模式无须处理");
        }
        
        if( $orderSupplier['way_bill_status'] == 1 ) {
            return Normal::returnCode(Normal::SUC,"当前订单已完成运单申报，无须再次处理");
        }
        
        //运单申报方式
        $declare_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('tomo_waybill');
        $mode = $declare_way_config['mode'];
        if( empty($mode) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商申报方式，请去中台系统-系统管理-业务设置-TOMO运单服务商配置！1");
        }
        
        if( empty($this->routerMap[$mode]) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商申报方式，请去中台系统-系统管理-业务设置-TOMO运单服务商配置！2");
        }
        $class = $this->routerMap[$mode].$mode ."ApiClient";
        if( !class_exists($class) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}不存在，请联系管理员核实!");
        }
        $method = 'createOrder';
        if( !method_exists($class, $method) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}方法{$method}不存在，请联系管理员核实!");
        }
        $client_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode($mode);
        if( empty($client_way_config) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商{$mode}申报方式，请去中台系统-系统管理-业务设置-{$mode}");
        }
        $params = [
            'binfo'         => $binfo,
            'order'         => $order,
            'supplierOrder' => $orderSupplier,
            'orderGoods'    => $orderGoods,
            'supplier'      => $supplier,
        ];
        
        $result = (new $class($client_way_config))->$method($params);
        if( $result['code'] == Normal::SUC ) 
        {
            $update_arr = [
                'way_bill'              => $mode,
                'way_bill_status'       => 1,
                'way_bill_time'         => time(),
                'way_bill_contents'     => json_encode($result['data'],JSON_UNESCAPED_UNICODE),
                'is_need_way_bill'      => 1,
            ];
        }
        else 
        {
            $update_arr = [
                'way_bill'              => $mode,
                'way_bill_status'       => 2,
                'way_bill_time'         => time(),
                'way_bill_contents'     => json_encode($result,JSON_UNESCAPED_UNICODE),
                'is_need_way_bill'      => 1,
            ];
        }
        
        $order_supplier_id   = (string)$orderSupplier['id'];
        OrderSupplier::instance()->where('id',$order_supplier_id)->update($update_arr);
        return $result;
    }
    
    /**
     * 查询haikou物流信息
     * @param array $orderSupplier
     * @return unknown[]|string[]|array[]|unknown
     */
    public function queryOrderAsyncExpressNotify(array $orderInfo)
    {
        //运单申报方式
        $declare_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('tomo_waybill');
        if( empty($declare_way_config['mode']) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商申报方式，请去中台系统-系统管理-业务设置-TOMO运单服务商配置！");
        }
    
        $mode = $declare_way_config['mode'];
        $class = $this->router . ucfirst($mode)."ApiClient";
        if( !class_exists($class) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}不存在，请联系管理员核实!");
        }
        $method = 'queryOrderAsyncExpressNotify';
        if( !method_exists($class, $method) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}方法{$method}不存在，请联系管理员核实!");
        }
        $client_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode($mode);
        if( empty($client_way_config) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商{$mode}申报方式，请去中台系统-系统管理-业务设置-{$mode}");
        }
        return (new $class($client_way_config))->$method($orderInfo);
    }
}